package com.dalin.platform.loa.action;

import java.util.ArrayList;
import java.util.List;

import com.dalin.common.utils.StringUtils;
import com.dalin.framework.action.StrutsTwoAction;
import com.dalin.platform.loa.LoaConst;
import com.dalin.platform.loa.condition.LoaOrganizationCondition;
import com.dalin.platform.loa.model.LoaOrganizationEntity;
import com.dalin.platform.loa.service.LoaService;
import com.dalin.platform.loa.vo.LoaOrganTreeVo;
/*****************************************************************************
 * @content: 类信息                                                   *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-06-07                                                     *
 * @modDate: 2010-06-07                                                     *
 * @version: 1.00                                                           *
 ****************************************************************************/
public class OrganizationQueryAction extends StrutsTwoAction{
    private LoaService loaService; // 
    private Long parentId;
    
    public String execute()throws Exception {
        super.printInfo("", "===== this action begin");
        try {
        	List<LoaOrganTreeVo> rtnList = new ArrayList<LoaOrganTreeVo>();
            LoaOrganizationCondition cond = new LoaOrganizationCondition();
            cond.setProjectId(getCurrentUser().getProjectId());
            cond.setParentId(parentId);
            List<LoaOrganizationEntity> entyList = loaService
            		.queryListByCond(cond.prepareParams());
            LoaOrganTreeVo vo = null;
            for (LoaOrganizationEntity enty : entyList) {
            	vo = new LoaOrganTreeVo();
            	vo.setId(enty.getOrganId()+"");
            	vo.setText(enty.getOrganName());
            	vo.setBelongAreaCode(enty.getBelongAreaCode());
            	vo.setOrganType(enty.getOrganType());
            	vo.setOrganTypeDisplay(LoaConst
            			.findItemName("organType", enty.getOrganType()));
            	vo.setParentId(enty.getParentId()+"");
            	vo.setRemark(enty.getDescription());
            	
            	// IS LEAF?
            	cond.setProjectId(getCurrentUser().getProjectId());
                cond.setParentId(enty.getOrganId());
            	List childrenList = loaService
            			.queryListByCond(cond.prepareParams());
            	if (StringUtils.isBlank(childrenList)) {
            		vo.setLeaf(true);
            	}
            	rtnList.add(vo);
            }
            setResContent(rtnList);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        super.printInfo("", "===== this action end");
        return null;

    }

    public LoaService getLoaService() {
        return loaService;

    }

    public void setLoaService(LoaService loaService) {
        this.loaService = loaService;

    }

	public Long getParentId() {
		return parentId == null ? LoaConst.BASE_PARENT_ID : parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}
}
